package com.qk.management.service.serviceimpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.enums.CommonVariablesEnum;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.domain.CourseDO;
import com.qk.dto.course.CustomerPageDTO;
import com.qk.dto.customer.CustomerInsertDTO;
import com.qk.entity.Customer;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import com.qk.vo.customer.CustomerByIdVO;
import com.qk.vo.customer.CustomerPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 上官徵羽
 * @version 1.0
 * @since 2025-10-19 21:46
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public PageResult<CustomerPageVO> listPage(CustomerPageDTO customerPageDTO) {
        Page<Customer> page = Page.of(customerPageDTO.getPage(), customerPageDTO.getPageSize());
        LambdaQueryWrapper<Customer> queryWrapper = Wrappers.lambdaQuery(Customer.class)
                .like(!ObjectUtil.isEmpty(customerPageDTO.getPhone()), Customer::getPhone, customerPageDTO.getPhone())
                .like(!ObjectUtil.isEmpty(customerPageDTO.getName()), Customer::getName, customerPageDTO.getName())
                .eq(!ObjectUtil.isEmpty(customerPageDTO.getChannel()), Customer::getChannel, customerPageDTO.getChannel())
                .eq(!ObjectUtil.isEmpty(customerPageDTO.getSubject()), Customer::getSubject, customerPageDTO.getSubject());
        Page<Customer> customerPage = this.baseMapper.selectPage(page, queryWrapper);
        Page<CustomerPageVO> customerPagePageVO = new Page<>();
        BeanUtil.copyProperties(customerPage, customerPagePageVO);
        if (!ObjectUtil.isEmpty(customerPage.getRecords())) {
            customerPagePageVO.setRecords(customerPage.getRecords().stream().map(customer -> {
                CustomerPageVO customerPageVO = new CustomerPageVO();
                BeanUtil.copyProperties(customer, customerPageVO);
                return customerPageVO;
            }).toList());
            List<Integer> courseIds = customerPage.getRecords().stream().map(Customer::getCourseId).distinct().toList();
            if (!ObjectUtil.isEmpty(courseIds)) {
                Map<Integer, String> courseNameMap = courseMapper
                        .selectList(Wrappers.lambdaQuery(CourseDO.class).in(CourseDO::getId, courseIds))
                        .stream()
                        .collect(Collectors.toMap(CourseDO::getId, CourseDO::getName));
                customerPagePageVO.getRecords().forEach(vo -> vo.setCourseName(courseNameMap.get(vo.getCourseId())));
            }
        }
        return PageResult.<CustomerPageVO>builder()
                .total(customerPage.getTotal())
                .rows(customerPagePageVO.getRecords())
                .build();
    }

    @Override
    public void insertCustomer(CustomerInsertDTO customerInsertDTO) {
        if (ObjectUtil.isEmpty(customerInsertDTO.getPhone())){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        Customer customer = BeanUtil.copyProperties(customerInsertDTO, Customer.class);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        this.baseMapper.insert(customer);
    }

    @Override
    public CustomerByIdVO getCustomerById(Integer id) {
        return BeanUtil.copyProperties(this.baseMapper.selectById(id), CustomerByIdVO.class);
    }

    @Override
    public void updateCustomer(Customer customer) {
        Customer customerSelect = query().eq(CommonVariablesEnum.PHONE.getKey(), customer.getPhone()).one();
        if (!ObjectUtil.isEmpty(customerSelect)&&!ObjectUtil.equals(customer.getId(), customerSelect.getId())){
            CommonException.throwCommonException(ParamEnum.PHONE_EXIST);
        }
        customer.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(customer);
    }
}
